//
//  GQHMeController.m
//  Xanthium
//
//  Created by GuanQinghao on 08/02/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHUserModel.h"

#pragma mark Object

#pragma mark View
#import "GQHMeView.h"

#pragma mark Controller
#import "GQHMeController.h"

#import "GQHTransactionController.h"
#import "GQHTruckController.h"
#import "GQHSettingsController.h"
#import "GQHReviewController.h"
#import "GQHOwnersFollowedController.h"
#import "GQHTrucksFollowedController.h"
#import "GQHQutationsController.h"
#import "GQHOrderController.h"
#import "GQHSuppliesController.h"

#import "GQHAuthorizationController.h"
#import "GQHOwnerAuthorizationController.h"
#import "GQHDriverAuthorizationController.h"
#import "GQHEnterpriseAuthorizationController.h"
#import "YYTransferController.h"

#import "GQHMembershipController.h"
#import "GQMyEmptyCarController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHMeController () <GQHMeViewDelegate> {
    
#pragma mark Data

    NSString * _currentVersion;
    
#pragma mark View
    /// 自定义根视图
    GQHMeView *rootView;
}

@end


@implementation GQHMeController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHMeView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    [self getAppVersion];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_leftButton.hidden = YES;
    self.qh_titleLabel.text = @"我的";
    [self.qh_rightButton setTitle:@"设置" forState:UIControlStateNormal];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [rootView.qh_tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
}

#pragma mark ---- 获取当前版本号
- (void)getAppVersion
{
    NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/version/1/lastVersion"];
    [GQHNetHelper qh_requestGET:URLString parameters:@{} success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        DLog(@"%@-%@",message,data);
        
        if (0 == [code intValue]) {
            
            _currentVersion = data[@"ios"];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self qh_alertWithTitle:nil message:errorString actionTitle:nil handler:nil completion:nil];
    }];
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
            
        case 0: {
            DLog(@"0");
            
            [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
                DLog(@"我的转帐");
                
                GQHTransactionController *transactionController = [[GQHTransactionController alloc] init];
                transactionController.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:transactionController animated:YES];
            } unallowed:nil];
        }
            break;
        case 1: {
            DLog(@"1");
            
            [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
                DLog(@"我的订单(司机)");
                
                /// 我的报价
                GQMyEmptyCarController *empty = [[GQMyEmptyCarController alloc] init];
                empty.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:empty animated:YES];
            } unallowed:nil];
        }
            break;
        case 2: {
            
            [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
                DLog(@"我的订单(司机)");
                
                GQHOrderController *orderController = [[GQHOrderController alloc] init];
                orderController.qh_orderState = @"待装货";
                orderController.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:orderController animated:YES];
            } unallowed:nil];
        }
            break;
        case 3: {
            DLog(@"2");
            
            [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
                DLog(@"我的订单(货主)");
                
                GQHOrderController *orderController = [[GQHOrderController alloc] init];
                orderController.qh_orderState = @"待装货";
                orderController.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:orderController animated:YES];
            } unallowed:nil];
        }
            break;
        case 4: {
            
            [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
                DLog(@"我的报价(司机)");
                
                GQHQutationsController *qutationsController = [[GQHQutationsController alloc] init];
                qutationsController.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:qutationsController animated:YES];
            } unallowed:nil];
        }
            break;
        case 5: {
            
            [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
                DLog(@"我的车辆(司机)");
                
                GQHTruckController *truckController = [[GQHTruckController alloc] init];
                truckController.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:truckController animated:YES];
            } unallowed:nil];
        }
            break;
        case 6: {
            
            [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
                DLog(@"我的货源(货主)");
                
                GQHSuppliesController *suppliesController = [[GQHSuppliesController alloc] init];
                suppliesController.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:suppliesController animated:YES];
            } unallowed:nil];
        }
            break;
        case 7: {
            
            [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
                DLog(@"我的评价");
                
                GQHReviewController *reviewController = [[GQHReviewController alloc] init];
                reviewController.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:reviewController animated:YES];
            } unallowed:nil];
        }
            break;
        case 8: {
            
            [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
                DLog(@"我关注的货主(司机)");
                
                GQHOwnersFollowedController *ownersFollowedController = [[GQHOwnersFollowedController alloc] init];
                ownersFollowedController.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:ownersFollowedController animated:YES];
            } unallowed:nil];
        }
            break;
        case 9: {
            
            [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
                DLog(@"我关注的车辆(货主)");
                
                GQHTrucksFollowedController *trucksFollowedController = [[GQHTrucksFollowedController alloc] init];
                trucksFollowedController.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:trucksFollowedController animated:YES];
            } unallowed:nil];
        }
            break;
        case 10: {
            if ([USER_GET_STATUS isEqualToString:USER_STATUS_S0] || [USER_GET_STATUS isEqualToString:USER_STATUS_S4])
            {
                if ([GET_RESULT_STATE isEqualToString:@"0"])
                {
                    [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
                        DLog(@"换车");
                        YYTransferController *transfer = [[YYTransferController alloc] init];
                        transfer.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:transfer animated:YES];
                    } unallowed:nil];
                }
                else
                {
                    [self showToastWithText:@"车辆信息正在审核中，请耐心等待"];
                }
            }
        }
            break;
        default:
            break;
    }
}

// 查看认证信息
- (void)qh_checkAuthorizedInformation {
    
    GQHAuthorizationController *authorizationController = [[GQHAuthorizationController alloc] init];
    authorizationController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:authorizationController animated:YES];
}

// 提交认证信息
- (void)qh_submitAuthorizedInformation {
    
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_O]) {
        
        GQHOwnerAuthorizationController *ownerAuthorizationController = [[GQHOwnerAuthorizationController alloc] init];
        ownerAuthorizationController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:ownerAuthorizationController animated:YES];
        
        return;
    }
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        GQHDriverAuthorizationController *driverAuthorizationController = [[GQHDriverAuthorizationController alloc] init];
        driverAuthorizationController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:driverAuthorizationController animated:YES];
        
        return;
    }
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_E]) {
        
        GQHEnterpriseAuthorizationController *enterpriseAuthorizationController = [[GQHEnterpriseAuthorizationController alloc] init];
        enterpriseAuthorizationController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:enterpriseAuthorizationController animated:YES];
        
        return;
    }
}

// 查看会员信息
- (void)qh_checkMembership {
    
    [self qh_alertWithTitle:@"" message:@"该功能尚未开通" actionTitle:nil handler:nil completion:nil];
}

#pragma mark --TargetMethod
- (void)qh_clickedRightButton:(UIButton *)sender {
    
    GQHSettingsController *settingsController = [[GQHSettingsController alloc] init];
    settingsController.hidesBottomBarWhenPushed = YES;
    settingsController.appVersion = _currentVersion;
    [self.navigationController pushViewController:settingsController animated:YES];
}

#pragma mark --PrivateMethod

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View


@end
